﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CFM.API;
using CFM.DTO;
using CFM.Common;
using System.Text.RegularExpressions;

namespace CFM
{
    public partial class frmTableGroup : Form
    {
        private TableGroupAPI _api = null;
        public frmTableGroup()
        {
            InitializeComponent();
            _api = new TableGroupAPI();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            TableGroup tableGroup = grvTableGroup.Rows[e.RowIndex].DataBoundItem as TableGroup;
            txtTableGroupID.Text = tableGroup.TableGroupId;
            txtTableGroupName.Text = tableGroup.TableGroupName;
            EnableControl(false);
            MethodHelper.ClearErrorProvider();

            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
            btnSave.Enabled = false;
        }

        private void frmTableGroup_Load(object sender, EventArgs e)
        {
            EnableControl(false);

            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            btnSave.Enabled = false;

            BindDataGridView();
        }
        private void BindDataGridView()
        {
            grvTableGroup.DataSource = _api.GetAll();
        }
        private void ResetControl()
        {
            txtTableGroupName.Text = string.Empty;
            txtTableGroupID.Text = string.Empty;
            btnSave.Enabled = true;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            txtTableGroupName.Focus();
            MethodHelper.ClearErrorProvider();
        }
        private void EnableControl(bool flag)
        {
            txtTableGroupID.Enabled = flag;
            txtTableGroupName.Enabled = flag;
        }
        private bool CheckBeforeSave()
        {
            bool flag = true;
            if (txtTableGroupID.Enabled && txtTableGroupID.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtTableGroupID, Const.EMPTY_MESSAGE);
                flag = false;
            }
            if (txtTableGroupName.Enabled && txtTableGroupName.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtTableGroupName, Const.EMPTY_MESSAGE);

                flag = false;
            }
            if (!txtTableGroupName.Enabled && !txtTableGroupID.Enabled) //2 cái đều groupid
                flag = false;
            return flag;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtTableGroupID.Text.Length > 0)
            {
                EnableControl(true);
                txtTableGroupID.Enabled = false;
                MethodHelper.ClearErrorProvider();

                btnSave.Enabled = true;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn xóa", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (txtTableGroupID.Text.Length > 0)
                {
                    _api.Delete(txtTableGroupID.Text.Trim());
                    BindDataGridView();
                    if (_api.Delete(txtTableGroupID.Text.Trim()) == false)
                        {
                            MessageBox.Show("Không thể xóa!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    ResetControl();

                    btnDelete.Enabled = false;

                    btnEdit.Enabled = false;
                } 
            }
            
            
        }

        private void txtTableGroupName_TextChanged(object sender, EventArgs e)
        {
            if (txtTableGroupID.Enabled == true)
            {
                txtTableGroupID.Text = MethodHelper.GetIdFromName(txtTableGroupName.Text);
            }
        }

        private void btnAdd_Click_1(object sender, EventArgs e)
        {
            EnableControl(true);
            ResetControl();

            btnSave.Enabled = true;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false; 
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckBeforeSave())
                return;
            TableGroup tableGroup = new TableGroup();
            tableGroup.TableGroupId = txtTableGroupID.Text;
            tableGroup.TableGroupName = txtTableGroupName.Text;
            if (txtTableGroupID.Enabled)
                _api.Create(tableGroup);
            else
                _api.Update(tableGroup);
            BindDataGridView();
            ResetControl();
            MethodHelper.ClearErrorProvider();

            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            
        }


    }
}
